package com.zxkj.commonpatrol.repoisity;

import com.allen.library.base.BaseHttpUtil;
import com.allen.library.interceptor.Transformer;
import com.allen.library.observer.CommonObserver;
import com.czy.basecommon.utils.base.BaseRepository;
import com.czy.basecommon.utils.constant.ItrackConstant;
import com.czy.basecommon.utils.tools.GsonUtil;
import com.czy.basecommon.utils.tools.SPUtils;
import com.zxkj.commonpatrol.model.post.InspectPropertys;
import com.zxkj.commonpatrol.model.post.PostSaveProject;
import com.zxkj.commonpatrol.model.post.PostSearchProject;
import com.zxkj.commonpatrol.model.response.RouteCustomInfoResponse;
import com.zxkj.commonpatrol.model.response.SearchProjectResponse;
import com.zxkj.commonpatrol.net.ETFApiHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.RequestBody;
import okhttp3.ResponseBody;

public class MProfileRepoisity extends BaseRepository {
    public MProfileRepoisity() {

    }

    public  void getData(String RouteInspectNo,String RouteTypeCode,MyBaseDataCallBack mDataCallBack){
        HashMap<String,Object> map=new HashMap<String, Object>();
        map.put("LoginId", SPUtils.get(ItrackConstant.LOGINID,""));
        map.put("Token", SPUtils.get(ItrackConstant.TOKEN,""));
        map.put("Lang", 1);
        PostSearchProject postSearchProject  =new PostSearchProject();
        postSearchProject.setRouteInspectNo(RouteInspectNo);
        postSearchProject.setRouteTypeCode(RouteTypeCode);
        ETFApiHelper.geRadomApi().GetRouteCustomInfo(map,new BaseHttpUtil<PostSearchProject>().convertVo2Json(postSearchProject))
                .compose(Transformer.<List<RouteCustomInfoResponse>>switchSchedulers())
                .subscribe(new CommonObserver<List<RouteCustomInfoResponse>>() {
                    @Override
                    protected String setTag() {
                        return "MProfile";
                    }

                    @Override
                    protected void onError(String errorMsg) {
                        mDataCallBack.onDataFailCallBack(404,errorMsg);
                    }

                    @Override
                    protected void onSuccess(List<RouteCustomInfoResponse> s) {

                        //回调数据
                        mDataCallBack.onDataSuccessCallBack(s);
                    }
                });


    }

    public void Save(String RouteInspectNo, List<String> datas, List<String> Code, MyBaseDataCallBack mDataCallBack){
        HashMap<String,Object> map=new HashMap<String, Object>();
        map.put("LoginId", SPUtils.get(ItrackConstant.LOGINID,""));
        map.put("Token", SPUtils.get(ItrackConstant.TOKEN,""));
        map.put("Lang", 1);
        List<PostSaveProject>postSaveProjects=new ArrayList<>();
        for (int i = 0; i < datas.size(); i++) {
            PostSaveProject postSearchProject  =new PostSaveProject();
            postSearchProject.setRouteInspectNo(RouteInspectNo);
            postSearchProject.setPropertyCode(Code.get(i));
            postSearchProject.setPropertyValue(datas.get(i));
            postSaveProjects.add(postSearchProject);
        }
//        InspectPropertys bean=new InspectPropertys();
//        bean.setInspectPropertys(GsonUtil.getGson().toJson(postSaveProjects));
        ETFApiHelper.geRadomApi().SaveRouteCustomInfo(map,new BaseHttpUtil<>().toJson(GsonUtil.getGson().toJson(postSaveProjects)))
                .compose(Transformer.<ResponseBody>switchSchedulers())
                .subscribe(new CommonObserver<ResponseBody>() {
                    @Override
                    protected String setTag() {
                        return "MProfile";
                    }

                    @Override
                    protected void onError(String errorMsg) {
                        mDataCallBack.onDataFailCallBack(404,errorMsg);
                    }

                    @Override
                    protected void onSuccess(ResponseBody s) {

                        //回调数据
                        mDataCallBack.onDataSuccessCallBack(s);
                    }
                });


    }


}
